using Game.Core;
using Game.Main.Model;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class SunItem : BaseItem
	{
		public enum State
		{
			Sun = 0,
			Moon = 1
		}

		public enum AnimState
		{
			Static = 0,
			Changing = 1
		}

		public State state;

		private AnimState animState;

		private Transform sun;

		private Transform moon;

		private bool isBrighten;

		private float percent;

		private float startAngle;

		private float moveTime;

		private float startX = 3f;

		private float width;

		private new float curAngle;

		private new float targetAngle;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			sun = model.Find("Sun");
			moon = model.Find("Moon");
			state = (State)data.state;
			if (state == State.Moon)
			{
				model.localEulerAngles = new Vector3(0f, 0f, 180f);
				sun.gameObject.SetActive(false);
			}
			Singleton<CameraManager>.Instance.Bind("game.camera_pos_change", OnCameraPositionChange);
		}

		protected override void LateUpdate()
		{
			if (animState != AnimState.Changing)
			{
				return;
			}
			moveTime += Time.deltaTime;
			percent = moveTime / 4f;
			if (percent >= 1f)
			{
				percent = 1f;
			}
			curAngle = Mathf.LerpAngle(startAngle, targetAngle, percent);
			model.localEulerAngles = new Vector3(0f, 0f, curAngle);
			if (curAngle >= targetAngle)
			{
				animState = AnimState.Static;
				if (state == State.Sun)
				{
					moon.gameObject.SetActive(false);
				}
				else
				{
					sun.gameObject.SetActive(false);
				}
			}
			if (isBrighten)
			{
				GlobalEventSystem.Fire(new BaseEvent("game.change_sun_light", percent));
			}
			else
			{
				GlobalEventSystem.Fire(new BaseEvent("game.change_sun_light", 1f - percent));
			}
		}

		public void OnCameraPositionChange(BaseEvent evt)
		{
			UpdatePosition((float)evt.Parameters);
		}

		public void InitWidth(float layerWidth)
		{
			width = layerWidth - 2f * startX;
		}

		public void UpdatePosition(float percent)
		{
			base.transform.localPosition = new Vector3(startX + width * percent, base.transform.localPosition.y, base.transform.localPosition.z);
		}

		public override void TriggerTouchUpEvent()
		{
			if (animState == AnimState.Static)
			{
				PlayChangeAnim();
			}
		}

		public void PlayChangeAnim()
		{
			animState = AnimState.Changing;
			sun.gameObject.SetActive(true);
			moon.gameObject.SetActive(true);
			if (state == State.Sun)
			{
				isBrighten = false;
				curAngle = 0f;
				startAngle = curAngle;
				targetAngle = 180f;
				state = State.Moon;
				GlobalEventSystem.Fire(new BaseEvent("game.change_sun_state", false));
			}
			else
			{
				isBrighten = true;
				curAngle = 180f;
				startAngle = curAngle;
				targetAngle = 360f;
				state = State.Sun;
				GlobalEventSystem.Fire(new BaseEvent("game.change_sun_state", true));
			}
			moveTime = 0f;
		}

		public void OnDestroy()
		{
			Singleton<CameraManager>.Instance.UnBind("game.camera_pos_change", OnCameraPositionChange);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			if (state != 0)
			{
				obj["33"] = (int)state;
			}
		}
	}
}
